function QT = ObjFctGMMmulti2(x);
global P ERROR_SERIES INSTRUMENTS N_ITER WEIGHT_WT_2 PAR_N_ITER_2;

% OBJ_FCT_GMM_MULTI_2.M           Version I.K. - 22/12/06
%   Constructs the objective function involved in the GMM estimation 
%   for multivariate loss parameters:
%
%       L(p,alpha,e) = 0.5*(norm(e,p)+t'*e)*norm(e,p)^(p-1)
%
% where
%   forecast error e = (e_1,...,e_d)'
%   exponent p is scalar
%   asymmetry vector t = (2*alpha_1-1,...,2*alpha_d-1)'
%  
% NOTATION (Hamilton, Ch14) & COMMENTS:
% 	x = [p, alpha_1, ..., alpha_N]'            
% 	Vt = K-VECTOR OF INSTRUMENTS with K >= N+1
% 	h(x,Vt) = []'
% 	g(x) = mean(h(x,Vt))		( eq 14.1.10 )
% 	Q(x) = g(x)'*WT*g(x)		( eq 14.1.11 )
% 	where WT = Id(rxr) at the first iteration (cf. Hamilton pg413)
% The series used in the objective function is the global variable ARMA_SERIES.
% This objective function is called several times and the weighting matrix WEIGHT_WT
% (global) changes at each iteration. The number of iterations is stored in the global
% variable N_ITER and the estimator obtained at iteration N_ITER is stored in
% the global variable PAR_N_ITER.

T = size(ERROR_SERIES,1);
N = size(ERROR_SERIES,2);
p = P;
alpha = x(1:(N));
tau = 2*alpha - 1;
% Computing the gradient N x T matrix of the multivariate loss L(p,alpha,e) wrt e
norm_p = (sum((abs(ERROR_SERIES)).^(p*ones(T,N)),2)).^(1/p*ones(T,1));
norm_p_pm1 = norm_p.^((p-1)*ones(T,1));
nu_p = [];
for i=1:1:N
    nu_p = [nu_p, (sign(ERROR_SERIES(:,i))).*(abs(ERROR_SERIES(:,i)).^(p-1))];
end
grad_p = p*nu_p + norm_p_pm1*(tau')+(((p-1)*ERROR_SERIES*tau./norm_p)*ones(1,N)).*nu_p;

% Computing the vector function h(x,Vt) 
h = [];
for i=1:1:T
    h = [h; kron(grad_p(i,:),INSTRUMENTS(i,:))];   
end
% computing the function g(x)
g = sum(h)'/size(h,1);	
% computing the weighting matrix WT 
if N_ITER == 1
   WT_2 = eye(size(g,1),size(g,1));
else
   WT_2 = WEIGHT_WT_2; 
end
% computing the objective function to minimize QT(x) 
QT = g'*WT_2*g;               % eq. 14.1.11 
